package classload;

import java.io.*;

public class MyClassLoad extends ClassLoader{

    /**
     * 对应一个应用的路径
     */
    private String path;

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        String replace = name.replace(".", "/");
        String classPath = path + replace + ".calss";
        try (InputStream in = new FileInputStream(classPath)){
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            int i = 0;
            while ((i = in.read()) != -1){
                out.write(i);
            }
            byte[] bytes = out.toByteArray();
            return defineClass(bytes,0,bytes.length);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
